<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="com.logistics.distribution.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <base href="<%=basePath%>">
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<script type="text/javascript" src="scripts/jquery/jquery-1.7.1.js"></script>
	<link href="style/authority/basic_layout.css" rel="stylesheet" type="text/css">
	<link href="style/authority/common_style.css" rel="stylesheet" type="text/css">
	<script type="text/javascript" src="scripts/authority/commonAll.js"></script>
	<script type="text/javascript" src="scripts/fancybox/jquery.fancybox-1.3.4.js"></script>
	<script type="text/javascript" src="scripts/fancybox/jquery.fancybox-1.3.4.pack.js"></script>
	<link rel="stylesheet" type="text/css" href="style/authority/jquery.fancybox-1.3.4.css" media="screen"></link>
	<script type="text/javascript" src="scripts/artDialog/artDialog.js?skin=default"></script>
    
    <title>新增订单</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">


		<%
			int i=0;
			double price=0;
			double discount=0;
			ArrayList<Product> arr=(ArrayList<Product>)request.getAttribute("products");
			if(arr==null) arr=new ArrayList<Product>();
		%>
	<script type="text/javascript" src="scripts/Myscripts/alpha_add.js"></script>
   <script language="javascript" type="text/javascript">
		function changecategory1()
		{
			var name1=document.getElementById("category_lv1").value;
			//document.getElementById("label1").innerHTML="servlet/Neworderservlet?type=query&name1="+name1+"&name2=all";
			document.form1.action="servlet/Neworderservlet?type=query&name1="+name1+"&name2=all";
			document.form1.submit();
		}
		function changecategory2()
		{
			var name1=document.getElementById("category_lv1").value;
			var name2=document.getElementById("category_lv2").value;
			document.form1.action="servlet/Neworderservlet?type=query&name1="+name1+"&name2="+name2;
			document.form1.submit();
		}

		function countprice()
		{
			//document.getElementById("tot_price").value=0;
			var p_id=document.getElementById("product_name").value;
			var p_number=document.getElementById("product_number").value;
			if(p_number==null) p_number=0; 
			<%
			i=0;
			price=0;
			discount=0;
			for(i=0;i<arr.size();i++)
			{
			%>
				if(p_id==<%=arr.size()==0?"error":arr.get(i).getProduct_id()%>)
				{
					<%
					price=arr.size()==0?0:Double.parseDouble(arr.get(i).getPrice());
					discount=arr.size()==0?0:Double.parseDouble(arr.get(i).getDiscount());
					%>
					document.getElementById("total_price").innerHTML=p_number*<%=discount*price%>;
					document.getElementById("tot_price").value=p_number*<%=discount*price%>;
				}
			<%
			}
			%>
		if(p_number<=0)
		{
			
			return false;
		}
			else
			return true;
		}
		
		function checkreceiver()
		{
		    //var temp = document.getElementById("receiver").value;
		    var temp = form1.receiver.value;
			if(temp=="") 
			{
				document.getElementById("receiver1").innerHTML="<font color='red'>收货人不能为空</font>";
				document.getElementById("receiver").focus();
				return false;
			}
			else{				
				document.getElementById("receiver1").innerHTML="<font color='green'>√</font>";
				return true;
			}
		}
		function checkcellphone(){
			var mobile=form1.cellphone.value;
			var pattern=/^\d{11}$/;
			if(!pattern.test(mobile))
			{
				document.getElementById("cellphone1").innerHTML="<font color='red'>请正确填写移动电话</font>";
				document.getElementById("cellphone").focus();
				return false;
			}else{
				
				document.getElementById("cellphone1").innerHTML="<font color='green'>√</font>";
				return true;
			}
		}	
		
	
		function checkaddress()
		{
			if(document.getElementById("address").value=="") 
			{
				document.getElementById("address1").innerHTML="<font color='red'>收货地址不能为空</font>";
				document.getElementById("address").focus();
				return false;
			}
			else{
				document.getElementById("address1").innerHTML="<font color='green'>√</font>";
				return true;
			}
		}
		function checkzipcode(){
		var zipcode=document.form1.zipcode.value;
		var pattern=/^\d{6}$/;
		var flag=false;
		if(pattern.test(zipcode)){
			flag=true;
			}else{
			flag=false;
			}
		if(flag){
			document.getElementById("zipcode1").innerHTML="<font color='green'>√</font>";
			return true;
		}else{
			document.getElementById("zipcode1").innerHTML="<font color='red'>请正确填写邮编</font>";
			document.getElementById("zipcode").focus();
			return false;
		}
		}
		
		function clicksubmit()
		{
			if(checkreceiver()&&checkcellphone()&&checkaddress()&&checkzipcode())
			{
				document.form1.action="servlet/Neworderservlet?type=insert&name1=&name2=";
				document.form1.submit();
			}
			else
			{
				alert("订单信息未完成！");
				return false;
			}
			
		}
		function clickreset()
		{
			/* document.getElementById("category_lv1").innerHTML="";
			document.getElementById("category_lv2").innerHTML="";
			document.getElementById("product_name").innerHTML="";
			document.getElementById("product_number").innerHTML="";*/
			document.getElementById("total_price").innerHTML="";  
			document.getElementById("receiver1").innerText="";
			document.getElementById("cellphone1").innerText="";
			document.getElementById("address1").innerText="";
			document.getElementById("zipcode1").innerText="";
		}
	</script>
  </head>
  
  <body>
   <form name="form1" method="post">   
   <div id = "add_alpha_id">
   		<div id="container">	
			<div class="ui_content">
				<div class="ui_tb"> 	
    	<table class="table" width="100%" align="center">
    		<tr><th colspan="6" align="center">订单基本信息</th></tr>
    		<tr>
    			<th>一级分类</th><th>二级分类</th><th>商品名称</th><th>商品数量</th><th>总价</th><th></th>
    		</tr>
    		<tr>
    			<td>
    				<select class="ui_select01" name="category_lv1" id="category_lv1" onChange="changecategory1()">
    					<option value="all" selected="selected"></option>
	              		<c:forEach items="${category1}" var="temp">
	              			<c:if test="${temp==optioncategory1}"><option value="${temp}" selected="selected">${temp} </option></c:if>
	              			<c:if test="${temp!=optioncategory1}"><option value="${temp}">${temp} </option></c:if>	              			
	              		</c:forEach>
    				</select>
    			</td>
    			
    			<td>
    				<select class="ui_select01" name="category_lv2" id="category_lv2" onChange="changecategory2()">
	              		<c:forEach items="${category2}" var="temp">
	              			<c:if test="${temp==optioncategory2}"><option value="${temp}" selected="selected">${temp} </option></c:if>
	              			<c:if test="${temp!=optioncategory2}"><option value="${temp}">${temp} </option></c:if>	              			
	              		</c:forEach>    				
    				</select>
    			</td>

    			<td>
    				<select class="ui_select01" name="product_name" id="product_name" onChange="countprice()">
	              		<c:forEach items="${products}" var="temp">
	              			<option id="${temp.product_id}" value="${temp.product_id}">${temp.product_name} </option>      			
	              		</c:forEach>    				
    				</select>
    			</td>
				<td><input class="ui_input_txt02" id="product_number" name="product_number" type="text" onblur="countprice()"/></td>			
    			<td><label id="total_price"></label></td>
    			<input type="hidden" name="tot_price" id="tot_price"/>
    			<td></td>
    		</tr>
    		<tr>
    			<th>收货人</th>   
				<td><input class="ui_input_txt02" id="receiver" name="receiver"  type="text" onblur="checkreceiver()"/></td>			
    			<td><div id="receiver1"></div></td>
    			<th>联系电话</th>   
				<td><input class="ui_input_txt02" id="cellphone" name="cellphone"  maxlength="11" type="text" onblur="checkcellphone()"/></td>
				<td><div id="cellphone1"></div></td>			
    		</tr>    	
    		<tr>
    			<th>收货地址</th>
    			<td><input class="ui_input_txt02" id="address" name="address" type="text" onblur="checkaddress()"/></td>
    			<td><div id="address1"></div></td>
    			<th>邮编</th>
    			<td>
    				<input class="ui_input_txt02" id="zipcode" name="zipcode" type="text" onblur="checkzipcode()"/>
    			</td>
    			<td><div id="zipcode1"></div></td>
    		</tr>	
    		<tr>
    			<th>订单注释</th>
    			<td><input class="ui_input_txt02" name="tips" size="64%" type="text"></td>
    			<td></td>
    			<th>订单日期</th>   
				<td><input id="order_date" class="ui_input_txt02" name="order_date" value="<%=new SimpleDateFormat("yyyy-MM-dd").format(new Date()) %>" type="text"/></td>			
    			<td></td>
    		</tr>
    		
  
    		<tr>
	    	    <td colspan="2" align="center"><input class="ui_input_btn01" value="提交" type="button" onclick="clicksubmit()"/></td>
	    		 <td></td>
	    		<td colspan="2" align="center"><input class="ui_input_btn01" value="重置" type="reset" onclick="clickreset()"/></td> 
    		</tr>  	
    	</table>
    	</div>
    	</div>
    	</div>
    	</div>
    </form>
    <script type="text/javascript">
    	if(${success=="yes"})alert("下订单成功");
    </script>
     <script type="text/javascript">
    var eitem = document.getElementById("add_alpha_id");
    window.onload = add_alpha(eitem);
    </script>
  </body>
</html>
